// pages/myCoupon/myCoupon.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
   page: {
      pageNo: 0,
      pageSize: 8
    },
    coupons: [],
    hidden: false,
    scrollTop: 0,
    scrollHeight: 0,
    hasMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    this.queryList();
  },
  queryList:function(){
    let that = this;
    wx.request({
      url: getApp().globalData.host + 'bizportal/my/buyerCoupon/list',
      data: { pageSize: that.data.page.pageSize, pageIndex: that.data.page.pageNo},
      header: getApp().globalData.header,
      method: 'POST',
      success: function (res) {
        let coupons = that.data.coupons;
        if (res.data.records == '' || res.data.records == null || typeof(res.data.records)=='undefined' ||res.data.records.length==0) {
          that.setData({
            hidden: true,
            hasMore: false
          });
          if(coupons.length>0)
            return;
        }
        for (let i = 0; i < res.data.records.length; i++) {
          res.data.records[i].itemExpireTime = util.formatDate(res.data.records[i].itemExpireTime);
          coupons.push(res.data.records[i]);
        }
        that.data.page.pageNo++;
        that.setData({
          coupons: coupons,
          hidden: true,
          hasMore:true
        });
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },
    //页面滑动到底部
  bindDownLoad: function () {
    if (this.data.hasMore) {
      this.queryList();
      //查询期间设置为不得再次查询
      this.setData({
        hasMore: false
      });

    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})